﻿using System;
using System.Linq;
using System.Linq.Expressions;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.ObjectModel;
using System.Data;

namespace ASPItalia.ModelVirtualCasting.EntityFramework {
	public class UserRepository : BaseRepository<User>, IUserRepository {
		public UserRepository(IContextContainer container)
			: base(container) {
		}

		public void InsertOnSubmit(User entity) {
			CurrentObjectSet.AddObject(entity);
		}

		public void Update(User entity) {
			CurrentObjectSet.Attach(entity);
			Context.ChangeObjectState(entity, EntityState.Modified);
		}

		public void DeleteOnSubmit(User entity) {
			CurrentObjectSet.DeleteObject(entity);
		}

		public void SubmitChanges() {
			SaveChanges();
		}

		public IQueryable<User> Where(Expression<Func<User, bool>> predicate) {
			return CurrentObjectSet.Where(predicate);
		}

		public long Count(Expression<Func<User, bool>> predicate) {
			return CurrentObjectSet.Count(predicate);
		}

		public User GetByUsername(string username) {
			return CurrentObjectSet.First(u => u.Username == username);
		}

		public IQueryable<User> GetAll() {
			return CurrentObjectSet;
		}

		public IUserRepository Include(string path) {
			CurrentObjectSet.Include(path);
			return this;
		}

		public IUserRepository EnableLazyLoading() {
			SetLazyLoading(true);
			return this;
		}

		public IUserRepository DisableLazyLoading() {
			SetLazyLoading(false);
			return this;
		}
	}
}